<?php

class admin_controller_system_menu extends admin_controller
{

	public function listing()
	{
		$group_id = get('group_id', 0, 'int');

		$model_menu = $this->get_admin_model();
		$model_menu_group = $this->get_admin_model('menu_group');
		
		$groups = $model_menu_group->get_groups();
		if($group_id==0) $group_id = $groups[0]->id;
		
		$template = $this->get_admin_template();
        $template->set_title('菜单列表');
        $template->set('menus', $model_menu->get_menus($group_id) );
        $template->set('group_id', $group_id );
		$template->set('groups', $groups );
		$template->display();
	}
	
    public function save()
    {
        $group_id = post('group_id', 0, 'int');

        $ids = post('id', array(), 'array(int)');
        $parent_ids = post('parent_id', array(), 'array(int)');
        $names = post('name', array(), 'array(string)');
        $urls = post('url', array(), 'array(string)');
        $targets = post('target', array(), 'array(string)');
        
        if (count($ids)>0)
        {
            for ($i = 0, $n = count($ids); $i < $n; $i++)
            {
                if (!($ids[$i] || $names[$i])) continue;
                
                $table_menu = $this->get_admin_table();
                $table_menu->id = $ids[$i];
                $table_menu->group_id = $group_id;
                $table_menu->parent_id = $parent_ids[$i];
                $table_menu->name = $names[$i];
                $table_menu->url = $urls[$i];
                $table_menu->target = $targets[$i];
                $table_menu->ordering = $i;
                $table_menu->save();
            }
        }
        
        $model_menu = $this->get_admin_model();
        $model_menu->update($group_id);

        $table_group = $this->get_admin_table('menu_group');
        $table_group->load($group_id);
        system_log('修改菜单组：'.$table_group->name);

        $this->redirect('./?action=system.menu.listing&group_id='.$group_id, '保存菜单成功');
    }
    

    public function delete()
    {
        $id = get('id', 0, 'int');
        if (!$id)
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '参数{id}丢失');
        }
        else
        {
            $table_menu = $this->get_admin_table();
            $table_menu->load($id);
            
            $model = $this->get_admin_model();
            if ($model->delete($id))
            {
                $this->set_ajax('status', 0);
                $this->set_ajax('description', '删除成功');
                
                system_log('删除菜单 #'.$id.' '.$table_menu->name);
            }
            else
            {
                $this->set_ajax('status', 2);
                $this->set_ajax('description', $model->get_error());
            }
        }
        $this->ajax();
    }
    
    public function set_link()
    {
        $id = get('id', 0, 'int');
        $new = get('id', 1, 'int');
        
		$template = $this->get_admin_template();
        $template->set_title('设置菜单链接');
        
        if($new==0)
        {
            $table_menu = $this->get_admin_table();
            $table_menu->load($id);
            $template->set('menu', $table_menu );
        }
        
        $model_app = $this->get_admin_model('app');
		$apps = $model_app->get_apps();
        $template->set('apps', $apps );
		
		$template->display();
		

    }
    
}
?>